package com.ct.main;

import com.ct.dao.Employee;
import com.ct.dao.MemberShip;
import com.ct.utils.JDBCUtils;
import com.ct.utils.Keyboard;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.rowset.SqlRowSet;

import java.lang.reflect.Member;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class Init {
    public static void main(String[] args) {
        membershipLogin();
    }
    static JdbcTemplate jdbcTemplate=new JdbcTemplate(JDBCUtils.ds);
    public static Employee employeeLogin(){
        Employee emp = null;
        System.out.println("欢迎光临");
        System.out.println("店员登录界面");
        System.out.println("请输入用户名");
        String userid = Keyboard.keyCode();
        System.out.println("请输入密码");
        String password=Keyboard.keyCode();
        String sql="select * from employee where id = ? and password = ?";
        try{
             emp = jdbcTemplate.queryForObject(sql, new RowMapper<Employee>() {
                public Employee mapRow(ResultSet resultSet, int i) throws SQLException {
                    Employee e = new Employee();
                    e.setId(resultSet.getInt("id"));
                    e.setEname(resultSet.getString("ename"));
                    e.setPosition(resultSet.getInt("position"));
                    e.setIsEmployed(resultSet.getInt("isEmployed"));
                    return e;
                }
            }, userid, password);
        }catch (Exception e){
            System.out.println("用户名或者密码错误请重新输入");
        }
        return emp;
    }

    public static void membershipLogin(){
        System.out.println("输入会员卡号");
        String s = Keyboard.keyCode();
        String sql="select score from membership where idnumber = ?";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql, s);
        if (maps==null||maps.size()==0){
            System.out.println("卡号不正确");
        }else {
            Map<String, Object> stringObjectMap = maps.get(0);
            System.out.print("积分为：");
            System.out.println(stringObjectMap.get("score"));
        }
    }
}
